import 'package:flutter/material.dart';

class DefaultTabControllerSample extends StatelessWidget
{
  //选项卡数据
  final List<Tab> myTabs=<Tab>
  [
      Tab(text: '选项卡一',),
      Tab(text: '选项卡二',),
  ];

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new MaterialApp(
              title: '',
              home: DefaultTabController(length: myTabs.length, child:Scaffold(
                        appBar: AppBar(
                                //添加导航栏
                                bottom: TabBar(tabs: myTabs),
                        ),
                        //添加导航视图
                        body: TabBarView(children: myTabs.map((Tab tab){
                                return Center(child: Text(tab.text),);
                        }).toList()),
              )),
    );
  }

}

class TabBarSample extends StatelessWidget
{

      @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new MaterialApp(
                title: '',
                //添加DefaultTabController 关联TabBar以及TabBarView
                home: new DefaultTabController(
                    length:items.length, //传入选项卡数量
                    child: new Scaffold(
                                  appBar: new AppBar(
                                          title: new Text('TabBar选项卡示例'),
                                          bottom: new TabBar(
                                                  isScrollable: true,
                                                  tabs: items.map((ItemView item){
                                                          return new Tab(
                                                                icon: new Icon(item.icon),
                                                                text: item.title,
                                                          );

                                                  }).toList(),
                                          ),
                                  ),
                                  //添加选项卡视图
                                body: new TabBarView(children: items.map((ItemView item){//迭代显示选项卡视图
                                                      return new Padding(
                                                        padding: const EdgeInsets.all(16.0),
                                                        child: new SelectedView(item: item,),
                                                              );
                                }).toList()),
                          )
                  ),

    );
  }
}


//视图项数据
class ItemView
{
    const ItemView({this.title,this.icon});//构造方法
    final String title;
    final IconData icon;
}

//选项卡类目
const List<ItemView> items= const <ItemView>
        [
            const ItemView(title: '自驾',icon: Icons.directions_car),
            const ItemView(title: '自行车',icon: Icons.directions_bike),
            const ItemView(title: '轮船',icon: Icons.directions_boat),
            const ItemView(title: '公交车',icon: Icons.directions_bus),
            const ItemView(title: '火车',icon: Icons.directions_railway),
            const ItemView(title: '步行',icon: Icons.directions_walk),
];

//被选中的视图
class SelectedView extends StatelessWidget
{
    const SelectedView({Key key,this.item}):super(key:key);
     //视图数据
    final ItemView item;

    @override
  Widget build(BuildContext context) {
    final TextStyle textStyle=Theme.of(context).textTheme.display1;
    return new Card(
            color: Colors.white,
            child: new Center(
                      child: new Column(
                              mainAxisSize: MainAxisSize.min,//垂直方向最小化处理
                              crossAxisAlignment: CrossAxisAlignment.center,//水平方向居中对齐
                              children: <Widget>[
                                        new Icon(item.icon,size: 128.0,color:textStyle.color,),
                                        new Text(item.title,style: textStyle,),
                              ],
                      ),
            ),
    );
  }
}